
class Solution:
    def isValid(self, s: str) -> bool:
        # 定义括号映射关系：左括号 -> 右括号
        dic = {
            "{": "}",
            "[": "]",
            "(": ")",
            "?": "?"  # 哨兵值，防止空栈pop操作
        }
        
        # 初始化栈，使用哨兵值'?'避免空栈判断
        stack = ['?']
        
        for c in s:
            # 如果是左括号，压入栈中
            if c in dic:
                stack.append(c)
            # 如果是右括号，检查是否与栈顶左括号匹配
            elif dic[stack.pop()] != c:
                return False  # 不匹配则返回False
        
        # 最后栈中应该只剩下哨兵值'?'，表示所有括号都正确匹配
        return len(stack) == 1

if __name__ == "__main__":

    sol = Solution()

    s = "([{]})"

    print(sol.isValid(s))